package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCarSeriesMapper;
import org.linlinjava.litemall.db.dao.LitemallCarSeriesMapper1;
import org.linlinjava.litemall.db.domain.LitemallCarBrand;
import org.linlinjava.litemall.db.domain.LitemallCarSeries;
import org.linlinjava.litemall.db.domain.LitemallCarSeriesExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallCarSeriesService {
    @Resource
    private LitemallCarSeriesMapper seriesMapper;
    @Resource
    private LitemallCarSeriesMapper1 seriesMapper1;

    private LitemallCarSeries.Column[] columns = new LitemallCarSeries.Column[]{LitemallCarSeries.Column.id, LitemallCarSeries.Column.name, LitemallCarSeries.Column.group, LitemallCarSeries.Column.image};

    public List<LitemallCarSeries> query(Integer page, Integer limit, String sort, String order) {
        LitemallCarSeriesExample example = new LitemallCarSeriesExample();
        example.or().andDeletedEqualTo(String.valueOf(false));
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return seriesMapper.selectByExampleSelective(example, columns);
    }

    public List<LitemallCarSeries> selectSeriesList(String brandId) {
        LitemallCarSeriesExample example = new LitemallCarSeriesExample();
        example.or().andBrandIdEqualTo(Integer.valueOf(brandId));
        example.or().andDeletedEqualTo(String.valueOf(false));
        return seriesMapper.selectByExampleSelective(example, columns);
    }



    public List<LitemallCarSeries> query(Integer page, Integer limit) {
        return query(page, limit, null, null);
    }

    public LitemallCarSeries findById(Integer id) {
        return seriesMapper.selectByPrimaryKey(id);
    }

    public List<LitemallCarSeries> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        LitemallCarSeriesExample example = new LitemallCarSeriesExample();
        LitemallCarSeriesExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(String.valueOf(false));

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return seriesMapper.selectByExample(example);
    }

    public int updateById(LitemallCarSeries brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return seriesMapper.updateByPrimaryKeySelective(brand);
    }

    public void updateState(LitemallCarSeries series){

        series.setUpdateTime(LocalDateTime.now());
        seriesMapper.updateByPrimaryKey(series);

    }


    public void deleteById(Integer id) {
        seriesMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallCarSeries brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        seriesMapper1.insert(brand);
    }

    public List<LitemallCarSeries> all() {
        LitemallCarSeriesExample example = new LitemallCarSeriesExample();
        example.or().andDeletedEqualTo(String.valueOf(false));
        return seriesMapper.selectByExample(example);
    }
}
